package com.observer;

import java.util.Date;
import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 被观察者BObservered, 当被观察者BObservered发生变化时通知观察者AObserver改变状态
 * 
 * @author sh04799
 *
 */
public class BObservered extends Observable {
	private int data = 0;

	private static BObservered instance;
	private BObservered(){
	}
	public static BObservered getInstance(){
		if(instance==null){
			instance=new BObservered();
		}
	
		return instance;
		
	}
	public void setData(int i) {
		if (this.data != i) {
			this.data = i;
			setChanged();
		}
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				setChanged();
				notifyObservers(new Date().toString());
				System.out.println("被观察者 B");
			}
		}, 1000, 1000);
		// notifyObservers(getData());
		// 只有在setChange()被调用后，notifyObservers()才会去调用update()，否则什么都不干。
	}
}
